package com.http;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.os.AsyncTask;

import com.android.ocean.util.net.ErrorCode;
import com.android.ocean.util.net.NetConUtil;
import com.android.ocean.util.net.ResEntity;
import com.android.ocean.util.net.ResObj;
import com.common.method.CommonData;
import com.common.method.ServerPath;
import com.modle.AuthorizeStruct;
import com.util.JsonUtil;

/**
 * 
 * @ClassName:AuthorizeGetTask
 * @Description:TODO获取2用户之间的权限
 * @author zhuw
 * @date 2013-9-29 下午10:38:10
 */
public class AuthorizeGetTask extends AsyncTask<String, Void, ResultAuthorize> {
	public static interface CallBack extends NetWorkCallBack {
		public abstract void procAuthGetSus(AuthorizeStruct struct);
		public abstract void procAuthGetFailed();

	}

	CallBack callback;
	public AuthorizeGetTask(CallBack callback) {
		super();
		this.callback = callback;
	}

	@Override
	protected ResultAuthorize doInBackground(String... params) {
		ResultAuthorize data = null;
		ArrayList<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
		paramPairs.add(new BasicNameValuePair("uid", params[0]));
		paramPairs.add(new BasicNameValuePair("tid", params[1]));
		try {
			ResEntity re=NetConUtil.postByForm(ServerPath.AUTHORIZE_GET_PATH,paramPairs);
			if(re.code==ResEntity.S_OK){
			    data= new ResultAuthorize(CommonData.H_FLAG_SUCCESS, null, JsonUtil.parseAuthorizeStruct(new JSONObject(re.jsonStr)));
			}else if(re.code==ResEntity.S_FAILED){
				
				  data= new ResultAuthorize(CommonData.H_FLAG_ERROR, "code:"+re.code+"    "+re.jsonStr, null);
				
			}else{
				  data= new ResultAuthorize(CommonData.H_FLAG_ERROR, "code:"+re.code+ErrorCode.ERROR_MSG, null);
			}
		} catch (Exception e) {
			data= new ResultAuthorize(CommonData.H_FLAG_ERROR, ErrorCode.ERROR_MSG, null);
			e.printStackTrace();
		}
		return data;
	}

	@Override
	protected void onPostExecute(ResultAuthorize result) {
		if (result.flag == CommonData.H_FLAG_SUCCESS) {
			callback.procAuthGetSus(result.data);

		} else {
			callback.procAuthGetFailed();
		}

	}
}
class ResultAuthorize  extends ResObj{
	public AuthorizeStruct data;
	public ResultAuthorize(int flag, String msg, AuthorizeStruct data) {
		super();
		this.flag = flag;
		this.msg = msg;
		this.data = data;
	}
	
	
}
